package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CollectAdapter;
import flc.ast.databinding.ActivityCollectBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.bean.StkResBean;

public class CollectActivity extends BaseAc<ActivityCollectBinding> {

    private CollectAdapter collectAdapter;
    private List<StkResBean> listShow=new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_collect;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCollectBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvCollectList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        collectAdapter=new CollectAdapter();
        mDataBinding.rvCollectList.setAdapter(collectAdapter);
        collectAdapter.addChildClickViewIds(R.id.ivCollectItemCollect);
        collectAdapter.setOnItemChildClickListener(this);
        collectAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean=collectAdapter.getItem(position);
        BaseWebviewActivity.open(mContext,bean.getUrl(),bean.getName());
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()){
            case R.id.ivCollectItemCollect:
                listShow.remove(collectAdapter.getItem(position));
                AppSpUtils.saveCollectList(listShow);
                getData();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData(){
        listShow.clear();
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            collectAdapter.setList(listShow);
            mDataBinding.rvCollectList.setVisibility(View.VISIBLE);
            mDataBinding.llCollectNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvCollectList.setVisibility(View.GONE);
            mDataBinding.llCollectNoData.setVisibility(View.VISIBLE);
        }
    }

}